package com.httpclient;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class TestAli {

    /**
     * @param args
     */
    public static void main(String[] args) {

        int returnCode = 0;
        GetMethod getFirst = new GetMethod("http://guahao.zjol.com.cn/VerifyCodeCH.aspx?0.41348255879557794");
        PostMethod post = new PostMethod();
        HttpClient client = new HttpClient();
        Cookie[] cookies = null;
        try {
            String result_string = "";
            int code = 1000;
            returnCode = client.executeMethod(getFirst);
            InputStream inputstream = getFirst.getResponseBodyAsStream();
            BufferedInputStream bis = new BufferedInputStream(inputstream); 
            FileOutputStream fos = new FileOutputStream("c:\\log\\a.jpeg"); 
            BufferedOutputStream bos = new BufferedOutputStream(fos);  
            int read;  
            while( (read = bis.read()) != -1 ){  
              bos.write(read);  
            }  
            bos.close();//不关闭，输出流不刷新，有可能得到无效图片  o
            
            while ("验证码已过期".equalsIgnoreCase(result_string) || "".equalsIgnoreCase(result_string) && code < 1010) {
                String url = "http://guahao.zjol.com.cn/ashx/LoginDefault.ashx?idcode=33032819740212493X&pwd=qwe123&txtVerifyCode=";
                url += (code++) % 10000;
                getFirst = new GetMethod(url);
                // 开始进行登录验证信息提交， 关键是注意参数的顺序
                returnCode = client.executeMethod(getFirst);
                // post.setDoAuthentication( true );
                cookies = client.getState().getCookies();
                //
                // for (int i = 0; i < cookies.length; i++) {
                // System.out.println(code+"cookiename==" +
                // cookies[i].getName());
                // System.out.println(code+"cookieValue==" +
                // cookies[i].getValue());
                // System.out.println(code+"Domain==" + cookies[i].getDomain());
                // System.out.println(code+"Path==" + cookies[i].getPath());
                // System.out.println(code+"Version==" +
                // cookies[i].getVersion());
                // }
                System.out.println(code + "==" + getFirst.getResponseBodyAsString());

            }
            for (int i = 0; i < cookies.length; i++) {
                System.out.println("2cookiename==" + cookies[i].getName());
                System.out.println("2cookieValue==" + cookies[i].getValue());
                System.out.println("2Domain==" + cookies[i].getDomain());
                System.out.println("2Path==" + cookies[i].getPath());
                System.out.println("2Version==" + cookies[i].getVersion());
            }

            // post = new
            // PostMethod("http://guahao.zjol.com.cn/DoctorInfo.Aspx?DEPART=69&ID=436");
            //
            // returnCode = client.executeMethod(post);
            // cookies = client.getState().getCookies();
            //
            // for (int i = 0; i < cookies.length; i++) {
            // System.out.println("3cookiename==" + cookies[i].getName());
            // System.out.println("3cookieValue==" + cookies[i].getValue());
            // System.out.println("3Domain==" + cookies[i].getDomain());
            // System.out.println("3Path==" + cookies[i].getPath());
            // System.out.println("3Version==" + cookies[i].getVersion());
            // }
            // System.out.println(post.getResponseBodyAsString());

        } catch (Exception e) {

        }

    }

    public static void requestInterceptor(HttpMethod method) {
        method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;");
        method.setRequestHeader("Accept-Language", "zh-cn");
        // αװ�����������
        method.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        method.setRequestHeader("Accept-Charset", "UTF-8");
        method.setRequestHeader("Keep-Alive", "300");
        method.setRequestHeader("Connection", "Keep-Alive");// close
        method.setRequestHeader("Cache-Control", "no-cache");
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
}
